package csd.wyaiyr.com.utils;


import csd.wyaiyr.com.config.BusinessException;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

public class CommonUtil {
    //获取两个日期之间的月份 倒叙
    public static List<String> getMonthBetweenDate(String endTime, String startTime) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<Date> list = new ArrayList<>();
        try {
            String startFlag="0";
            String endFlag="0";

            if(startTime.endsWith("01")){
                 startFlag="1";
            }
            if(endTime.endsWith("30")||endTime.endsWith("31")||endTime.endsWith("02-28")||endTime.endsWith("02-29")){
                endFlag="1";
            }


            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);


            //开始时间不能小于2022年8月
            //结束时间不能大于当月
            Date limitStartDate = sdf.parse("2022-08");
            Date limitEndDate = sdf.parse(DateUtil.formatDate(new Date(), DateUtil.DateMode_15));

            if(startDate.getTime()<limitStartDate.getTime()){
                startDate=limitStartDate;
            }
            if(endDate.getTime()>limitEndDate.getTime()){
                endDate=limitEndDate;
            }

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            if("1".equals(startFlag)){
                calendar.setTime(startDate);
                //1号时获取上个月
                calendar.add(Calendar.MONTH, -1);

                list.add(calendar.getTime());
            }

            calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {

                // 把日期添加到集合
                list.add(startDate);

                // 设置日期
                calendar.setTime(startDate);

                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);

                // 获取增加后的日期
                startDate = calendar.getTime();
            }

            if("1".equals(endFlag)){
                list.add(calendar.getTime());
            }

        } catch (Exception e) {
            ;
        }
        List<String> voList=new ArrayList<>();
        list.sort(Comparator.comparing(Date::getTime).reversed());
        for (Date date : list) {
            voList.add(sdf.format(date).replace("-",""));
        }

        return voList;
    }




    public static String addZeroForStr(String str, int length,int type) {
        int strLen = str.length();
        if (strLen < length) {
            while (strLen < length) {
                StringBuffer sb = new StringBuffer();
                if(type==1){
                    // 左补0
                    sb.append("0").append(str);
                }else if(type==2){
                    //右补0
                    sb.append(str).append("0");
                }
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

    public static String getTabNum(){
        int i = new Date().getMonth() + 1;
        String month="";
        if(i<10){
            month="0"+i;
        }else{
            month=i+"";
        }
        String tabNum = new Date().getYear() + 1900 + "" + month;
        return tabNum;
    }



    public static  String getYesterdayTabNum(){
        if(new Date().getDate()>1){
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
            String lastMonth = dft.format(cal.getTime());
            return lastMonth;
        }else{
            Calendar cal = Calendar.getInstance();
            cal.add(cal.MONTH, -1);
            SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
            String lastMonth = dft.format(cal.getTime());
            return lastMonth;
        }

    }

    public static String getTabNumByMinesDay(int days){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.DATE, days);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }

    public static String getDateByMinesDay(int days){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.DATE, days);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }

    public static String getDD_DateByMinesDay(int days){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.DATE, days);
        return cal.getTime().getDate()+"";
    }

    public static String getMonthByMinesDay(int days){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.DATE, days);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }





    public static  String getTabNumByCreateTime(Date createTime){
        return DateUtil.formatDate(createTime, DateUtil.DateMode_14);
    }


    public static  String getLastMonth(){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, -1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }

    public static  String getLastMonthYYYY_MM(){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, -1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }

    public static  String getLastMonthFirstDay(){
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }


    public CommonUtil() {
    }

    public static List<String> get8day(int dayCount){
        List<String> days=new ArrayList<>();
        for(int i=dayCount;i>0;i--){
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DATE, 0-i);
            Date date2 = cal.getTime();
            SimpleDateFormat format2= new SimpleDateFormat("yyyy-MM-dd");
            String dateStringYYYYMMDD2 = format2.format(date2);
            days.add(dateStringYYYYMMDD2);
        }
        return  days;
    }




    public static List<String> getMonthList(int count){
        List<String> months=new ArrayList<>();
        for(int i=count;i>0;i--){
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.MONTH, 0-i);
            Date date2 = cal.getTime();
            SimpleDateFormat format2= new SimpleDateFormat("yyyy-MM");
            String dateStringYYYYMMDD2 = format2.format(date2);
            months.add(dateStringYYYYMMDD2);
        }
        return  months;
    }


    public static String getLastDateOfMonth(String month){
        Date date = DateUtil.convertToMonth(month);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }


    public static  List<String> getMonthList(int count,String month){
        List<String> months=new ArrayList<>();
        count-=1;
        Date date = DateUtil.convertToMonth(month);
        for(int i=count;i>=0;i--){
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, 0-i);
            Date date2 = cal.getTime();
            SimpleDateFormat format2= new SimpleDateFormat("yyyy-MM");
            String dateStringYYYYMMDD2 = format2.format(date2);
            months.add(dateStringYYYYMMDD2);
        }
        return  months;

    }

    public static String getLastMonthOfMonth(String month){
        Date date = DateUtil.convertToMonth(month);

        Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, -1);
            Date date2 = cal.getTime();
            SimpleDateFormat format2= new SimpleDateFormat("yyyy-MM");
            String dateStringYYYYMMDD2 = format2.format(date2);
            return dateStringYYYYMMDD2;
    }


    public static List<String> getMonthFullDay(String date) {
        List<String> fullDayList = new ArrayList<String>();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = 1;// 所有月份从1号开始
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);// 1月从0开始
        cal.set(Calendar.DAY_OF_MONTH, day);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int j = 0; j <= (count - 1); ) {
            if (simpleDateFormat.format(cal.getTime()).equals(getLastDay(year, month)))
                break;
            cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
            j++;
            fullDayList.add(simpleDateFormat.format(cal.getTime()));
        }
        return fullDayList;
    }

    public static  String getLastDay(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        return simpleDateFormat.format(cal.getTime());
    }


    /**
     * 去除对象所有string类型字段的空格
     * @param obj
     */
    public static void trimFields(Object obj)  {
        if (obj == null) {
            return;
        }

        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            try {
            if (field.getType().equals(String.class)) {
                field.setAccessible(true);
                String value = (String) field.get(obj);
                if (value != null) {
                    value = value.replaceAll(" ","");

                        field.set(obj, value);

                }
            }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public  CommonUtil isNotNull(Object obj,String msg){
        if(obj==null){
            throw new BusinessException(msg);
        }
        if(StringUtil.isEmpty(obj+"")){
            throw new BusinessException(msg);
        }

        return this;
    }


}
